<?php

namespace App\Console\Commands;

use App\AdvertiserImg;
use App\Service\DunImageService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class ScheduleQueryImage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'schedule:queryImage';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info("定时任务queryImage开始", [
            'timestamp' => time()
        ]);
        $imageService = DunImageService::getInstance();
        $imageService->setConfig(
            \config('app.dun_image_secret_id'),
            \config('app.dun_image_secret_key'),
            \config('app.dun_image_business_id')
        );
        AdvertiserImg::query()->where('status', 1)->chunkById(20, function ($dataList) use ($imageService){
            $dataList = $dataList->toArray();
            $taskIds = array_column($dataList, 'task_id');
//            $taskIds = ['gpd3841yjembltzy4h6fo90h0030aa69'];
            $params = array(
                "taskIds"=>json_encode($taskIds)
            );
            $ret = $imageService->query($params);
            if (isset($ret['code']) && $ret['code'] == 200) {

                foreach ($ret['result'] as $item){
//                    if (isset($item['status']) && $item['status'] == 0){
                        $dataId = $item['name'] ?? '';
                        if (!empty($dataId)){
                            $labelsArr = [];
                            if (isset($item['labels'])  && $item['labels']){
                                foreach ($item['labels'] as $label){
                                    if ($label['level'] > 0){
                                        $labelsArr[] = [
                                            'label' => $label['label'],
                                            'tagName' => AdvertiserImg::LABEL[$label['label']] ?? ''
                                        ];
                                    }
                                }
                            }

                            $model = AdvertiserImg::query()
                                ->where('image_id', $dataId)
                                ->where('status', 1)
                                ->first();
                            if ($model){
                                $model->status = 2;
                                $model->suggestion = $item['suggestion'] ?? 0;
                                $model->censor_time = Carbon::now();
                                $model->label = json_encode($labelsArr);
                                $model->save();
                            }
                        }
//                    }
                }
            }

            usleep(2000);
        });

        return 0;
    }
}
